﻿// ----------------------------------------------------------------------------------------------
// Copyright (c) Microsoft.
// ----------------------------------------------------------------------------------------------
// This source code is subject to terms and conditions of the Microsoft Public License. A 
// copy of the license can be found in the License.html file at the root of this distribution. 
// If you cannot locate the  Microsoft Public License, please send an email to 
// dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
//  by the terms of the Microsoft Public License.
// ----------------------------------------------------------------------------------------------
// You must not remove this notice, or any other, from this software.
// ----------------------------------------------------------------------------------------------

namespace Samples
{
    using System.Runtime.Serialization;

    partial class StatusCodeException
    {
        // --------------------------------------------------------------------
        // StatusCodeException consists of two partial classes, the generated 
        // partial class and this partial class which is manually written.

        // Partial classes are merged during compile-time.

        // The purpose of the manually written partial class is to add a 
        // field (StatusCode) to StatusCode exception and using partial 
        // methods add custom serialization

        // Partial is very useful to a T4 developer that likes to generate code
        // but would like to allow for some customization of the generated code. 
        // Changing the generated code directly is a bad idea as it will just 
        // be overwritten next time it's generated. 

        // In addition to partial classes there's something called partial 
        // methods. A partial method can be thought of as a event that is 
        // hooked up during compile-time as opposed to a normal .NET event 
        // which is hooked up during runtime.

        // A partial method is allowed to have no implementation and a call
        // to an empty partial method will be optimized away by the compiler

        // Partial methods must always be private and "return" void 

        // Partial Class Definitions
        // http://msdn.microsoft.com/en-us/library/wa80x488(v=VS.100).aspx

        // partial (Type)
        // http://msdn.microsoft.com/en-us/library/wbx7zzdd(v=VS.100).aspx

        // partial (Method)
        // http://msdn.microsoft.com/en-us/library/6b0scde8.aspx

        // --------------------------------------------------------------------

        const string StatusCodeName = "StatusCode";

        public int StatusCode;

        partial void OnSerialize (SerializationInfo info, StreamingContext context)
        {
            info.AddValue (StatusCodeName, StatusCode);
        }

        partial void OnUnserialize (SerializationInfo info, StreamingContext context)
        {
            StatusCode = info.GetInt32 (StatusCodeName);
        }
    }
}
